package com.ebt.m.utils.js;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.text.TextUtils;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.Toast;

import com.ebt.m.R;
import com.ebt.m.activity.WebBaseActivity;
import com.ebt.m.commons.buscomponent.thirtylogin.model.ShareListener;
import com.ebt.m.data.rxModel.apibean.ShareParamsCustomeA;
import com.ebt.m.share.ShareDialog;
import com.ebt.m.share.ShareUtils;
import com.ebt.m.utils.ConfigData;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Damon.Han on 2018/7/18 0018.
 * 淞菱-易保云对接说明文档-3.6 3.7
 *
 * @author Damon
 */
public class JsObjectCustomeA {
    private Context context;
    private WebView webView;


    public JsObjectCustomeA(Context context) {
        this.context = context;
    }

    public JsObjectCustomeA(Context context, WebView webView) {
        this.context = context;
        this.webView = webView;
    }

    private Activity getActivity() {
        while (!(context instanceof Activity) && context instanceof ContextWrapper) {
            context = ((ContextWrapper) context).getBaseContext();
        }
        if (context instanceof WebBaseActivity) {
            return (WebBaseActivity) context;
        }
        if (context instanceof Activity) {
            return (Activity) context;
        }
        return null;
    }

    /**
     * 调用分享功能
     */
    @JavascriptInterface
    public void callNative(String params) {
        Activity activity = getActivity();
        if (activity == null) {
            return;
        }
        try {
            Gson gson = new Gson();
            ShareParamsCustomeA shareParms = gson.fromJson(params, ShareParamsCustomeA.class);
            if (shareParms == null) {
                return;
            }
            String action = shareParms.getAction();
            if ("share".equalsIgnoreCase(action)) {
                List<String> hideList = new ArrayList<>();
                hideList.add(ShareDialog.COPY_LINK);
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        share(activity, shareParms.getTargetUrl(), shareParms.getTitle(), shareParms.getDesc(), shareParms.getThumbUrl(), hideList);
                    }
                });
            } else if ("backAction".equalsIgnoreCase(action)) {
                //返回按钮
                if (webView != null && webView.canGoBack()) {
                    webView.goBack();
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void share(Activity activity, String url, String title, String subTitle, String imgUrl, List<String> hideList) {
        Context appContext = activity.getApplicationContext();
        ShareListener shareListener = new ShareListener() {
            @Override
            public void onShareError(String platform, String errorInfo) {
                if (!TextUtils.isEmpty(errorInfo)) {
                    Toast.makeText(appContext, errorInfo, Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(appContext, ShareUtils.getZhFromEn(platform, appContext)
                            + appContext.getString(R.string.share_failed), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onShareComplete(String platform) {
                Toast.makeText(appContext, ShareUtils.getZhFromEn(platform, appContext)
                        + appContext.getString(R.string.share_completed), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onShareCancel(String platform) {
                Toast.makeText(appContext, ShareUtils.getZhFromEn(platform, appContext)
                        + appContext.getString(R.string.share_canceled), Toast.LENGTH_SHORT).show();
            }
        };
        openShare(activity, url, title, subTitle, imgUrl, hideList, shareListener);
    }

    /**
     * 打开分享对话框
     *
     * @param activity
     * @param url
     * @param title
     * @param subTitle
     * @param imgUrl
     * @param hideList      不需要分享的平台
     * @param shareListener
     */
    private void openShare(Activity activity, String url, String title, String subTitle, String imgUrl, List<String> hideList, ShareListener shareListener) {
        try {
            if(TextUtils.isEmpty(imgUrl)){
                imgUrl = ConfigData.PORTRAITPATH + File.separator + "logo.png";
            }
            new ShareDialog.Builder(activity)
                    .setShareTitle(title)
                    .setShareContent(subTitle)
                    .setShareUrl(url)
                    .setShareUrlWeXin(url)
                    .setShareImagePath(imgUrl)
                    .setHidePlatformList(hideList)
                    .setShareListener(shareListener)
                    .setDialogDismissIfShareSuccess()
                    .create()
                    .show();
            EventBus.getDefault().post(shareListener);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
